package cn.wizzer.app.web.modules.controllers.crown.viewPoint;

import cn.wizzer.app.crown.modules.models.CrownViewPoint;
import cn.wizzer.app.crown.modules.models.enums.MatchTypeEnum;
import cn.wizzer.app.crown.modules.services.CrownMatchService;
import cn.wizzer.app.crown.modules.services.ViewPointOrderService;
import cn.wizzer.app.crown.modules.services.ViewPointService;
import cn.wizzer.app.sys.modules.models.SysMsg;
import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.UserCertify;
import cn.wizzer.app.user.modules.services.UserCertifyService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.notify.WkNotifyUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;


/**
 * 皇冠观点Controller
 *
 * @author
 */
@At("/api/match/hg/view/point")
@IocBean
@Filters({@By(type = ApiTokenFilter.class)})
public class ApiViewPointController {


    @Reference
    @Inject
    private ViewPointService viewPointService;

    @Reference
    @Inject
    private ViewPointOrderService viewPointOrderService;

    @Reference
    @Inject
    private UserCertifyService certifyService;

    @Reference
    @Inject
    private CrownMatchService crownMatchService;

    @Reference
    @Inject
    private SysParamService sysParamService;

    @Inject
    @Reference
    private WkNotifyUtil wkNotifyUtil;

    @Inject
    @Reference
    private UserService userService;

    @Inject
    @Reference
    private SysMsgService sysMsgService;


    @ApiOperation("认证身份为发布者")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户发布认证申请")
    public Result certification(@Param("..") UserCertify certify) {
        String appid = Mvcs.getReq().getHeader("appid");
        certify.setUserId(appid);
        certify.setCtAt(Times.getTS());
        //待审
        certify.setStatus(0);
        certifyService.insert(certify);

        User user = userService.fetch(appid);

        //构建消息通知
        SysMsg msg = sysMsgService.createMsg("system", "申请足篮认证",
                String.format("<p>消息内容:用户[%s]手机号[%s]于<span style='color:red'>%s</span> 申请足篮认证，请尽快确认。<a href=%s style='color:blue'>点击处理</a></p>", user.getUserName(), user.getPhone(), DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"), "/platform/user/certify?userName=" + user.getUserName()),
                null);
        //插入通知
        sysMsgService.saveMsg(msg, null);
        //后台通知管理员
        if (msg != null) {
            wkNotifyUtil.notify(msg, null);
        }
        return Result.success();
    }

    @ApiOperation("个人观点列表分页查询")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result myList(Integer pageNumber, int pageSize, MatchTypeEnum matchType, String userId) {
        return Result.success().addData(viewPointService.selfList(pageNumber, pageSize, Mvcs.getReq().getHeader("appid"), matchType, userId));
    }

    @ApiOperation("观点列表分页查询")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result list(Integer pageNumber, int pageSize, String searchKeyword, MatchTypeEnum matchType, String sortType) {
        return Result.success().addData(viewPointService.listPage(pageNumber, pageSize, searchKeyword, Mvcs.getReq().getHeader("appid"), matchType, sortType));
    }

    @ApiOperation("观点发布")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户发布观点", msg = "赛事: ${viewPoint.matchId}, 标题: ${viewPoint.title}")
    public Result add(@Param("..") CrownViewPoint viewPoint) {
        //发布观点返回消息
        SysMsg msg = viewPointService.add(viewPoint, Mvcs.getReq().getHeader("appid"));
        //后台通知管理员
        wkNotifyUtil.notify(msg, null);
        return Result.success();
    }

    @ApiOperation("观点-删除")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户删除观点", msg = "观点记录: ${id}")
    public Result del(String id) {

        viewPointService.update(Chain.make("delFlag", "1"), Cnd.where("id", "=", id));
        return Result.success();
    }

    @ApiOperation("观点-购买")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    @ULog(tag = "用户购买观点", msg = "观点记录: ${id}")
    public Result buy(String id) {
        viewPointOrderService.add(Mvcs.getReq().getHeader("appid"), id);
        return Result.success();
    }

    @ApiOperation("观点-详情")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result detail(String id) {
        return Result.successNonNull(viewPointService.detail(id, Mvcs.getReq().getHeader("appid")));
    }


    @ApiOperation("赛事")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result match(Integer pageNo, Integer pageSize, MatchTypeEnum matchType) {
        return Result.success(viewPointService.matchList(pageNo, pageSize, matchType));
    }

    @ApiOperation("关注用户观点列表查询")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result focusViewPoint(Integer pageNumber, Integer pageSize) {
        return Result.success(viewPointService.listPageVo(pageNumber, pageSize, Mvcs.getReq().getHeader("appid")));
    }

    @ApiOperation("战绩榜")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result recordList(Integer pageNumber, Integer pageSize, String type) {
        return Result.success(viewPointService.recordListPage(pageNumber, pageSize, type));
    }

    @ApiOperation("足篮专家")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result expertList(Integer pageNumber, Integer pageSize) {
        return Result.success(viewPointService.expertListPage(pageNumber, pageSize, Mvcs.getReq().getHeader("appid")));
    }

    @ApiOperation("我的粉丝")
    @At()
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Result funList(Integer pageNumber, Integer pageSize) {
        return Result.success(viewPointService.funList(pageNumber, pageSize, Mvcs.getReq().getHeader("appid")));
    }

}





